package com.cxhc.system.service.hcdoc.impl;

import com.cxhc.common.utils.DateUtils;
import com.cxhc.system.domain.hcdoc.HdBranchBudget;
import com.cxhc.system.domain.hcdoc.HdBranchBudgetModifyLog;
import com.cxhc.system.domain.hcdoc.HdBranchInfo;
import com.cxhc.system.domain.hcdoc.HdProjectInfo;
import com.cxhc.system.domain.hcdoc.HdUserInfo;
import com.cxhc.system.mapper.hcdoc.HdBranchBudgetMapper;
import com.cxhc.system.mapper.hcdoc.HdBranchBudgetModifyLogMapper;
import com.cxhc.system.mapper.hcdoc.HdBranchInfoMapper;
import com.cxhc.system.mapper.hcdoc.HdProjectInfoMapper;
import com.cxhc.system.service.hcdoc.HdBranchBudgetService;
import com.cxhc.system.service.hcdoc.HdProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 预算金额表 业务层处理
 *
 * @author Dhq
 */
@Service
public class HdBranchBudgetServiceImpl  implements HdBranchBudgetService {

    @Autowired
    private HdBranchBudgetMapper hdBranchBudgetMapper;

    @Autowired
    private HdBranchBudgetModifyLogMapper hdBranchBudgetModifyLogMapper;

    @Autowired
    private HdBranchInfoMapper hdBranchInfoMapper;

    @Autowired
    private HdProjectInfoMapper hdProjectInfoMapper;


    @Override
    public Map<String,Object> selectById(String id) {
        Map<String,Object> map = new HashMap<>();
        HdBranchBudget hdBranchBudget = hdBranchBudgetMapper.selectById(id);
        map.put("essentialInfo",hdBranchBudget);

        List<HdProjectInfo> hdProjectInfos = hdProjectInfoMapper.selectListForBudget(hdBranchBudget.getBranchInfoId(), DateUtils.dateTimeNow(DateUtils.YYYY));
        map.put("projects",hdProjectInfos);

        HdBranchBudgetModifyLog hdBranchBudgetModifyLog = new HdBranchBudgetModifyLog();
        hdBranchBudgetModifyLog.setBranchBudgetId(String.valueOf(hdBranchBudget.getId()));
        List<HdBranchBudgetModifyLog> hdBranchBudgetModifyLogs = hdBranchBudgetModifyLogMapper.selectList(hdBranchBudgetModifyLog);
        map.put("changeLog",hdBranchBudgetModifyLogs);

        return map;
    }

    @Override
    public HdBranchBudget selectByIdForProject(String branchInfoId) {
        return hdBranchBudgetMapper.selectByBranchInfoIdAndYear(branchInfoId,DateUtils.dateTimeNow(DateUtils.YYYY));
    }

    @Override
    public int insertHdBranchBudget(HdBranchBudget hdBranchBudget) {
        HdBranchInfo hdBranchInfo = hdBranchInfoMapper.selectById(hdBranchBudget.getBranchInfoId());
        if (ObjectUtils.isEmpty(hdBranchInfo)){
            throw new RuntimeException("预算单位不存在!!!");
        }
        hdBranchBudget.setAvailableAmount(hdBranchBudget.getBudgetAmount());
        return hdBranchBudgetMapper.insert(hdBranchBudget);
    }

    @Override
    public List<HdBranchBudget> selectList(HdBranchBudget hdBranchBudget) {
        return hdBranchBudgetMapper.selectList(hdBranchBudget);
    }

    @Override
    public int update(HdBranchBudget hdBranchBudget) {
        return hdBranchBudgetMapper.update(hdBranchBudget);
    }

    @Override
    public int updateBudgetAmount(HdBranchBudgetModifyLog hdBranchBudgetModifyLog) {
        synchronized (HdBranchBudgetServiceImpl.class){
            HdBranchBudget hdBranchBudget = hdBranchBudgetMapper.selectById(hdBranchBudgetModifyLog.getBranchBudgetId());
            //已经使用金额
            BigDecimal transferredAmount = new BigDecimal(hdBranchBudget.getTransferredAmount().replaceAll(",",""));
            //变更金额
            BigDecimal newBudgetAmount = new BigDecimal(hdBranchBudgetModifyLog.getNewBudgetAmount().replaceAll(",",""));
            //对比变更金额与可用金额
            if (newBudgetAmount.compareTo(transferredAmount) < 0){
                throw new RuntimeException("变更金额不可小于当前已用金额!!!");
            }

            BigDecimal availableAmount = newBudgetAmount.subtract(transferredAmount);
            HdBranchBudget hdBranchBudgetForUpdate = new HdBranchBudget();
            hdBranchBudgetForUpdate.setId(hdBranchBudget.getId());
            hdBranchBudgetForUpdate.setAvailableAmount(String.valueOf(availableAmount));
            hdBranchBudgetForUpdate.setBudgetAmount(hdBranchBudgetModifyLog.getNewBudgetAmount().replaceAll(",",""));
            hdBranchBudgetMapper.update(hdBranchBudgetForUpdate);
            return  hdBranchBudgetModifyLogMapper.insert(hdBranchBudgetModifyLog);
        }
    }
}